﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Telerik.Web.UI;
using Telerik.Web.UI.ImageEditor;

namespace RN.ERP.Web.UserControl
{
    public partial class FileUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdnObjectGuid.Value = Request.QueryString["o"];
                hdnSysCode.Value = Request.QueryString["s"];
                hdnFuncCode.Value = Request.QueryString["f"];
            }
        }

        protected void AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
        {
            //Clear changes and remove uploaded image from Cache 
            RadImageEditor1.ResetChanges();
            Context.Cache.Remove(Session.SessionID + "UploadedFile");
            using (Stream stream = e.File.InputStream)
            {
                byte[] imgData = new byte[stream.Length];
                stream.Read(imgData, 0, imgData.Length);
                MemoryStream ms = new MemoryStream();
                ms.Write(imgData, 0, imgData.Length);

                //byte[] bytes = ms.ToArray();
                Context.Cache.Insert(Session.SessionID + "UploadedFile", ms, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
            }
        }

        protected void RadImageEditor1_ImageLoading(object sender, ImageEditorLoadingEventArgs args)
        {
            //Handle Uploaded images 
            if (!Object.Equals(Context.Cache.Get(Session.SessionID + "UploadedFile"), null))
            {
                using (EditableImage image = new EditableImage((MemoryStream)Context.Cache.Get(Session.SessionID + "UploadedFile")))
                {
                    args.Image = image.Clone();
                    args.Cancel = true;
                }
            }
        }

        protected void RadImageEditor1_ImageSaving(object sender, ImageEditorSavingEventArgs e)
        {
            try
            {
                using (MemoryStream stream = (MemoryStream)Context.Cache.Get(Session.SessionID + "UploadedFile"))
                {
                    string objectGuid = hdnObjectGuid.Value.Trim() + "." + e.Image.Format;
                    string sysCode = hdnSysCode.Value.Trim();
                    string funcCode = hdnFuncCode.Value.Trim();
                    if ((!string.IsNullOrEmpty(objectGuid)) && (!string.IsNullOrEmpty(sysCode)) && (!string.IsNullOrEmpty(funcCode)))
                    {
                        byte[] bytes = stream.ToArray();
                        FileService.FileWebServiceSoapClient fileServiceClient = new FileService.FileWebServiceSoapClient();
                        ////上传服务返回结果：1,上传成功！；0,错误信息
                        string result = fileServiceClient.UploadImageBytes(bytes, objectGuid, sysCode, funcCode, true, "", "");
                        string[] results = result.Split(new char[] { ',' });
                        if (results.Length > 1)
                        {
                            RadWindowManager1.RadAlert(results[1], 300, 150, "提示", string.Empty);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                RadWindowManager1.RadAlert(ex.Message, 300, 150, "提示", string.Empty);
            }
            finally
            {
                e.Cancel = true;
            }
        }
    }
}